import HelloWorld from './HelloWorld';

import Input from './input';
import Icon from './icon';
import Modal from './modal';
import Toast from './modal/toast';
import Loading from './modal/loading';

import Radio from './radio';
import RadioGroup from './radio-group';
import Checkbox from './checkbox';
import CheckboxGroup from './checkbox-group';

/**
 * 组件库
 */
export default {
	HelloWorld,
	Modal,
	Toast,
	Loading,
	Icon,

	Input,

	Radio,
	RadioGroup,
	Checkbox,
	CheckboxGroup,
	
	install(Vue) {
		/**
		 * 注册组件
		 */
		Object.entries(this).forEach(([k, v]) => k == 'install' || Vue.component(`V${k}`, v));

		/**
		 * Toast
		 */
		Vue.prototype.$toast = function (context, duration) {
			return new Promise(resolve => {
				new Vue({
					el: document.createElement('template'),
					mounted() { this.$timeout(() => this.$refs.toast.open(), 100) },
					render(createElement) {
						return createElement('v-toast', {
							ref: 'toast', props: { duration },
							on: { closed: () => { this.$el.parentNode.removeChild(this.$el), resolve() } }
						}, context);
					}
				});
			});
		}

		/**
		 * Loading
		 */
		Object.defineProperty(Vue.prototype, '$loading', {
			get() {
				return loading.$refs.target;
			},
			set(value) {
				loading.$refs.target[value ? 'open' : 'close']();
			}
		});
		let loading = new Vue({
			el: document.createElement('template'),
			render(createElement) {
				return createElement('v-loading', { ref: 'target' });
			}
		});

	}
}